package com.ray.friendslist;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ray.friendslist.adaptor.ExpandableHeightGridView;
import com.ray.friendslist.adaptor.GridViewAdapter;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.feeds.FeedEntyty;
import com.ray.friendslist.images.ImageLoader;

public class VKFeedViewActivity extends FragmentActivity implements
		OnItemClickListener, LoaderCallbacks<Cursor> {

	private static final String WALL = "WALL";

	private static final String FEEDS = "FEEDS";

	private static final String FROM = "FROM";

	private static final String ITEM_ID = "ITEM_ID";

	private TextView mProfileNameTextView;
	private TextView mFeedContentTextView;
	private ImageView mProfilePhotoImageView;
	private TextView mDateTextView;
	private TextView mLikesTextView;
	private ExpandableHeightGridView mGridView;
	private GridViewAdapter mImageAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feed_view);
		initViews();
		// bind();
		mGridView.setExpanded(true);
		mGridView.setAdapter(mImageAdapter);
		mGridView.setOnItemClickListener(this);
		getSupportLoaderManager().initLoader(0, null, this);
	}

	private void initViews() {
		mProfileNameTextView = (TextView) findViewById(R.id.vk_view_feed_profile_name_text_view);
		mFeedContentTextView = (TextView) findViewById(R.id.vk_view_feed_content_text_view);
		mProfilePhotoImageView = (ImageView) findViewById(R.id.vk_view_feed_profile_photo_image_view);
		mDateTextView = (TextView) findViewById(R.id.vk_view_feed_date_text_view);
		mLikesTextView = (TextView) findViewById(R.id.vk_view_feed_likes_text_view);
		mGridView = (ExpandableHeightGridView) findViewById(R.id.vk_view_feed_images_grid_view);
	}

	private void bind(Cursor cursor) {
			mLikesTextView
					.setText("likes: "
							+ cursor.getString(cursor
									.getColumnIndex(FriendsContract.UserInfoColumns.LIKES)));
			mDateTextView.setText(cursor.getString(cursor
					.getColumnIndex(FriendsContract.UserInfoColumns.DATE)));
			mProfileNameTextView
					.setText(cursor.getString(cursor
							.getColumnIndex(FriendsContract.UserInfoColumns.PROFILE_NAME)));
			if (!TextUtils.isEmpty(cursor.getString(cursor
					.getColumnIndex(FriendsContract.UserInfoColumns.TEXT)))) {
				mFeedContentTextView.setText(cursor.getString(cursor
						.getColumnIndex(FriendsContract.UserInfoColumns.TEXT)));
				mFeedContentTextView.setVisibility(View.VISIBLE);
			}
			else{
				mFeedContentTextView.setVisibility(View.GONE);
			}
			setImage(
					cursor.getString(cursor
							.getColumnIndex(FriendsContract.UserInfoColumns.PROFILE_PHOTO)),
					mProfilePhotoImageView);
			mImageAdapter = new GridViewAdapter(
					this,
					FeedEntyty.convertFeedAttachmentsArray(cursor.getString(cursor
							.getColumnIndex(FriendsContract.UserInfoColumns.ATTACHMENT_PHOTO))));
			mGridView.setExpanded(true);
			mGridView.setAdapter(mImageAdapter);
			mGridView.setOnItemClickListener(this);
	}

	private void setImage(final String source, final ImageView view) {
		if (!TextUtils.isEmpty(source)) {
			ImageLoader.getInstance().bind(view, source);
			view.setVisibility(View.VISIBLE);
		} else {
			view.setImageBitmap(null);
			view.setVisibility(View.GONE);
		}
	}

	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		String value = (String) adapterView.getItemAtPosition(position);
		Intent intent = new Intent(view.getContext(), ImageViewActivity.class);
		intent.putExtra("IMAGE_SOURCE", value);
		view.getContext().startActivity(intent);
	}

	public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
		String where = null;
		String[] selectionArgs = null;
		Uri contentUri = null;
		if (getIntent().getStringExtra(FROM).equals(FEEDS)) {
			where = FriendsContract.VKFeedColoumns.ITEM_ID;
			selectionArgs = new String[] { getIntent().getStringExtra(ITEM_ID) };
			contentUri = FriendsContract.VKFeedColoumns.CONTENT_URI;
		}
		if (getIntent().getStringExtra(FROM).equals(WALL)) {
			where = FriendsContract.UserInfoColumns.ITEM_ID;
			selectionArgs = new String[] { getIntent().getStringExtra(ITEM_ID) };
			contentUri = FriendsContract.UserInfoColumns.CONTENT_URI;
		}
		Log.d("VKFeedViewActivity onCreateLoader",
				getIntent().getStringExtra(ITEM_ID));
		return new CursorLoader(this, contentUri, null, where + " = ?",
				selectionArgs, null);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		cursor.moveToFirst();
		bind(cursor);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		// do nothing
	}

}
